package com.cat.dataStructure02;

import java.util.HashSet;
import java.util.Set;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/longest-balanced-subarray-i/description/
 * @create 2025/10/19 17:13
 * @since JDK17
 */

public class Solution23 {
    public int longestBalanced(int[] nums) {
        int n = nums.length, ans = 0;
        for (int i = 0; i < n; i++) {
            Set<Integer> odd = new HashSet<>(), even = new HashSet<>();
            for (int j = i; j < n; j++) {
                if (nums[j] % 2 == 0) {
                    odd.add(nums[j]);
                } else {
                    even.add(nums[j]);
                }
                if (odd.size() == even.size()) {
                    ans = Math.max(ans, j - i + 1);
                }
            }
        }

        return ans;
    }
}
